package dao;

import entity.User;
import util.DatabaseUtil;
import java.sql.*;

public class UserDao {
    
    // 根据ID查询用户
    public User findById(Long id) throws SQLException {
        String sql = "SELECT * FROM users WHERE user_id = ?";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setLong(1, id);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return mapResultSetToUser(rs);
            }
        }
        return null;
    }
    
    // 根据用户名查询用户
    public User findByUsername(String username) throws SQLException {
        String sql = "SELECT * FROM users WHERE username = ?";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, username);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return mapResultSetToUser(rs);
            }
        }
        return null;
    }
    
    // 创建用户
    public User create(User user) throws SQLException {
        String sql = "INSERT INTO users (username, password, background_image_path) VALUES (?, ?, ?)";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            stmt.setString(1, user.getUsername());
            stmt.setString(2, user.getPassword());
            stmt.setString(3, user.getBackgroundImage());
            
            stmt.executeUpdate();
            ResultSet rs = stmt.getGeneratedKeys();
            if (rs.next()) {
                user.setUserId(rs.getLong(1));
            }
            return user;
        }
    }
    
    // 更新用户背景图片
    public boolean updateBackgroundImage(Long userId, String backgroundImage) throws SQLException {
        String sql = "UPDATE users SET background_image_path = ? WHERE user_id = ?";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, backgroundImage);
            stmt.setLong(2, userId);
            return stmt.executeUpdate() > 0;
        }
    }
    
    // 更新用户背景色
    public boolean updateBackgroundColor(Long userId, String backgroundColor) throws SQLException {
        String sql = "UPDATE users SET background_color = ? WHERE user_id = ?";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, backgroundColor);
            stmt.setLong(2, userId);
            return stmt.executeUpdate() > 0;
        }
    }
    
    // ResultSet映射到实体
    private User mapResultSetToUser(ResultSet rs) throws SQLException {
        User user = new User();
        user.setUserId(rs.getLong("user_id"));
        user.setUsername(rs.getString("username"));
        user.setPassword(rs.getString("password"));
        user.setBackgroundImage(rs.getString("background_image_path"));
        user.setBackgroundColor(rs.getString("background_color"));
        user.setCreatedAt(rs.getTimestamp("created_at"));
        return user;
    }
} 